package com.tanhua.management.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.management.service.MessagesService;
import com.tanhua.management.vo.MessagesDetailsVo;
import com.tanhua.management.vo.MessagesVo;
import com.tanhua.management.vo.Movements;
import com.tanhua.management.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author 王加鑫
 * @Date 2021/09/16 21:22
 * @Since JDK 1.8
 * 消息Controller
 */
@RestController
@RequestMapping("manage")
public class MessagesController {

    @Autowired
    private MessagesService messagesService;

    /**
     * 查看消息详情(消息表id)
     * @return
     */
    @GetMapping("messages/{id}")
    public ResponseEntity<MessagesDetailsVo> queryMessagesDetails(@PathVariable("id")String id) {
        try {
            MessagesDetailsVo messagesDetailsVo =  this.messagesService.queryMessagesDetails(id);
            if(ObjectUtil.isNotEmpty(messagesDetailsVo)){
                return ResponseEntity.ok(messagesDetailsVo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }


    /**
     * 查询消息翻页
     * @param pageSize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @param mid
     * @param startTime
     * @param endTime
     * @param userId
     * @param state
     * @return
     */
    @GetMapping("messages")
    public ResponseEntity<PageResult> queryMessagesList(@RequestParam(value = "pagesize") Integer pageSize,
                                                        @RequestParam(value = "page") Integer page,
                                                        @RequestParam(value = "sortProp",defaultValue = "createDate") String sortProp,
                                                        @RequestParam("sortOrder") String sortOrder,
                                                        @RequestParam(value = "id",required = false) String mid,
                                                        @RequestParam(value = "sd",required = false)String startTime,
                                                        @RequestParam(value = "ed",required = false)String endTime,
                                                        @RequestParam(value = "uid",required = false)Long userId,
                                                        @RequestParam(value = "state",required = false)String state
                                                        ) {

        try {
            PageResult pageResult = this.messagesService.queryMessagesList(pageSize,page,sortProp,sortOrder,mid,startTime,endTime,userId,state);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 消息通过
     * @param ids
     * @return
     */
    @PostMapping("messages/pass")
    public ResponseEntity<String> messagesPast(@RequestBody String[] ids) {
        try {
            String state = this.messagesService.messagesPast(ids);
            return ResponseEntity.ok(state);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 消息拒绝
     * @param ids
     * @return
     */
    @PostMapping("messages/reject")
    public ResponseEntity<String> messagesReject(@RequestBody String[] ids) {
        try {
            String state = this.messagesService.messagesReject(ids);
            return ResponseEntity.ok(state);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 消息撤销
     * @param ids
     * @return
     */
    @PostMapping("messages/revocation")
    public ResponseEntity<String> messagesRevocation(@RequestBody String[] ids) {
        try {
            String state = this.messagesService.messagesRevocation(ids);
            return ResponseEntity.ok(state);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 消息置顶
     * @param id
     * @return
     */
    @PostMapping("messages/{id}/top")
    public ResponseEntity<String> messagesTop(@PathVariable("id") String id) {
        try {
            String topState = this.messagesService.messagesTop(id);
            return ResponseEntity.ok(topState);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 取消置顶
     * @param id
     * @return
     */
    @PostMapping("messages/{id}/untop")
    public ResponseEntity<String> messagesUnTop(@PathVariable("id") String id) {
        try {
            String topState = this.messagesService.messagesUnTop(id);
            return ResponseEntity.ok(topState);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(500).build();
    }
}
